package com.buyouchina.web.action.product;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.buyouchina.bean.area.Province;
import com.buyouchina.bean.privilege.User;
import com.buyouchina.bean.product.Account;
import com.buyouchina.bean.product.Statistical;
import com.buyouchina.bean.product.Account.AccountAreaType;
import com.buyouchina.bean.product.Account.DateType;
import com.buyouchina.service.area.ProvinceService;
import com.buyouchina.service.product.AccountService;
import com.buyouchina.utils.DateUtils;
import com.buyouchina.utils.UserUtil;
import com.buyouchina.web.action.BaseAction;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

@Controller("AccountAction")
public class AccountAction extends BaseAction implements ModelDriven<Account>,
		Preparable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Resource(name = "accountServiceBean")
	private AccountService accountService;
	@Resource(name = "provinceServiceBean")
	private ProvinceService provinceService;
	private  Account account;
	private List<Account> accounts = new ArrayList<Account>();
	private List<Province> provinceList = new ArrayList<Province>();
	private List<Statistical> statisticals = new ArrayList<Statistical>();
	private Integer accountid;
	private Integer provinceid;

	private Date queryDate;
	/*
	private Date startTime;
	private Date endTime;
	*/
	private String startTime;
	private String endTime;
	private String areaName;

	public String defaultAccount() {
		User user = (User) session.get("user");
		queryDate = DateUtils.getPreviousDay();

		if (UserUtil.getMaxLevel(user) > 9000) {
			String hql = "from Account a where a.areaType=?1 and a.date=?2";
			accounts = accountService.list(hql, AccountAreaType.STATELEVEL,
					queryDate);
			provinceList = provinceService
					.list("from Province p where p.fullAddress is null");
		} else if (UserUtil.getMaxLevel(user) == 7000) {
			provinceid = new ArrayList<Province>(user.getProvinces()).get(0)
					.getId();
			String hql = "from Account a where a.areaType=?1 and a.date=?2 and a.province.id=?3";
			accounts = accountService.list(hql, AccountAreaType.COUNTYLEVEL,
					queryDate, provinceid);
			provinceList = provinceService.list(
					"from Province p where p.provinceid=?1", provinceid
							.toString());

		} else if (UserUtil.getMaxLevel(user) == 6000) {
			provinceid = new ArrayList<Province>(user.getProvinces()).get(0)
					.getId();
			String hql = "from Account a where a.areaType=?1 and a.date=?2 and a.province.id=?3";
			accounts = accountService.list(hql, AccountAreaType.CITYLEVEL,
					queryDate, provinceid);
			provinceList = provinceService.list(
					"from Province p where p.cityid=?1", provinceid.toString());
		}
		return SUCCESS;
	}

	public String query() {
		try {
			User user = (User) session.get("user");
			String hql = "from Account a where a.dateType=?1 and a.date between ?2 and ?3 and a.province.id=?4";
			if(account.getDateType().equals(DateType.DAY)){
				accounts = accountService.list(hql, account.getDateType(),DateUtils.getPreviousDay(),
						DateUtils.getPreviousDay(), provinceid);
			}else if(account.getDateType().equals(DateType.WEEK)){
				accounts = accountService.list(hql, account.getDateType(),DateUtils.getPreviousDayInterval()[0],
						DateUtils.getPreviousDayInterval()[1], provinceid);
			}else if(account.getDateType().equals(DateType.MONTH)){
				accounts = accountService.list(hql, account.getDateType(),DateUtils.getPreviousMonthInterval()[0],
						DateUtils.getPreviousMonthInterval()[1], provinceid);
			}else{
				accounts = accountService.list(hql, account.getDateType(), DateUtils.getPreviousYearInterval()[0],
						 DateUtils.getPreviousYearInterval()[0], provinceid);
			}
			
			if (UserUtil.getMaxLevel(user) > 9000) {
				hql = "from Account a where a.areaType=?1 and a.date=?2";
				provinceList = provinceService
						.list("from Province p where p.fullAddress is null");
			} else if (UserUtil.getMaxLevel(user) == 7000) {
				provinceid = new ArrayList<Province>(user.getProvinces()).get(0)
						.getId();
				hql = "from Account a where a.areaType=?1 and a.date=?2 and a.province.id=?3";
				provinceList = provinceService.list(
						"from Province p where p.provinceid=?1", provinceid
								.toString());

			} else if (UserUtil.getMaxLevel(user) == 6000) {
				provinceid = new ArrayList<Province>(user.getProvinces()).get(0)
						.getId();
				hql = "from Account a where a.areaType=?1 and a.date=?2 and a.province.id=?3";
				provinceList = provinceService.list(
						"from Province p where p.cityid=?1", provinceid.toString());
			}
			areaName=provinceService.find(provinceid).getName();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	// 统计列表
	public String list() throws ParseException {

		User user = (User) session.get("user");
		if (UserUtil.getMaxLevel(user) > 9000) {
			accounts = accountService.list(new Date(),
					AccountAreaType.STATELEVEL, null);
		} else if (UserUtil.getMaxLevel(user) == 7000) {
			provinceid = new ArrayList<Province>(user.getProvinces()).get(0)
					.getId();
			accounts = accountService.list(new Date(),
					AccountAreaType.COUNTYLEVEL, provinceid);
		} else if (UserUtil.getMaxLevel(user) == 6000) {
			provinceid = new ArrayList<Province>(user.getProvinces()).get(0)
					.getId();
			accounts = accountService.list(new Date(),
					AccountAreaType.CITYLEVEL, provinceid);
		}
		return SUCCESS;
	}

	/*
	// 查看核算详细
	public String getDetail() {
		account = accountService.find(accountid);

		if (account.getAreaType().equals(AccountAreaType.STATELEVEL)) {
			accounts = accountService.listByProduct(account.getProduct()
					.getId(), AccountAreaType.COUNTYLEVEL,
					AccountType.PURCHASE, new Date(), null);
		} else if (account.getAreaType().equals(AccountAreaType.COUNTYLEVEL)) {
			accounts = accountService.listByProduct(account.getProduct()
					.getId(), AccountAreaType.CITYLEVEL, AccountType.PURCHASE,
					new Date(), account.getProvince());
		} else {
			statisticals = new ArrayList<Statistical>(account.getTongjis());
			return "viewOrder";
		}
		return "view";
	}
*/
	public Date[] todayTimeArea() throws ParseException {
		Date[] dates = new Date[2];
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String startTime = sdf.format(new Date()) + " 00:00:00";
		String endTime = sdf.format(new Date()) + " 23:59:59";
		dates[0] = sdf1.parse(startTime);
		dates[1] = sdf1.parse(endTime);
		return dates;
	}

	public Account getModel() {
		return account;
	}

	public void prepare() throws Exception {
		if (accountid == null || "".equals(accountid)) {
			account = new Account();
		} else {
			account = accountService.find(accountid);
		}
	}

	public Account getAccount() {
		return account;
	}

	public void setAccount(Account account) {
		this.account = account;
	}

	public Integer getAccountid() {
		return accountid;
	}

	public void setAccountid(Integer accountid) {
		this.accountid = accountid;
	}

	public List<Account> getAccounts() {
		return accounts;
	}

	public void setAccounts(List<Account> accounts) {
		this.accounts = accounts;
	}

	public List<Province> getProvinceList() {
		return provinceList;
	}

	public void setProvinceList(List<Province> provinceList) {
		this.provinceList = provinceList;
	}

	public Integer getProvinceid() {
		return provinceid;
	}

	public void setProvinceid(Integer provinceid) {
		this.provinceid = provinceid;
	}

	public List<Statistical> getStatisticals() {
		return statisticals;
	}

	public void setStatisticals(List<Statistical> statisticals) {
		this.statisticals = statisticals;
	}

	public Date getQueryDate() {
		return queryDate;
	}

	public void setQueryDate(Date queryDate) {
		this.queryDate = queryDate;
	}


	public String getAreaName() {
		return areaName;
	}

	public void setAreaName(String areaName) {
		this.areaName = areaName;
	}

	public String getStartTime() {
		return startTime;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

}
